package priv.bigant.intranet.visual.win;

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import org.apache.commons.lang3.StringUtils;
import priv.bigant.intranet.client.ClientConfig;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.regex.Pattern;

public class ConfigWin extends JDialog {
    private JPanel contentPane;
    private JTextField hostNameField;
    private JTextField ipTextField;
    private JTextField portText;
    private JButton 提交Button;
    private JLabel defaultHostLab;
    private ClientConfig clientConfig;
    private ActionListener action;

    public void init() {
        initContext();
    }

    public void initContext() {
        if (StringUtils.isEmpty(clientConfig.getDefaultHost())) {
            defaultHostLab.setVisible(false);
        } else {
            defaultHostLab.setText(clientConfig.getDefaultHost());
        }

        提交Button.addActionListener(e -> {
            String hostName = hostNameField.getText();
            if (StringUtils.isEmpty(hostName)) {
                JOptionPane.showMessageDialog(this, "请输入域名", "域名错误", JOptionPane.ERROR_MESSAGE);
                return;
            }
            if (hostName.length() > 20) {
                JOptionPane.showMessageDialog(this, "域名长度不可超过20", "域名错误", JOptionPane.ERROR_MESSAGE);
                return;
            }

            String ip = ipTextField.getText();
            if (StringUtils.isEmpty(ip)) {
                JOptionPane.showMessageDialog(this, "请输入客户端IP", "客户端IP错误", JOptionPane.ERROR_MESSAGE);
                return;
            }
            if (!Pattern.matches("((1[0-9][0-9]\\.)|(2[0-4][0-9]\\.)|(25[0-5]\\.)|([1-9][0-9]\\.)|([0-9]\\.)){3}((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))", ip)) {
                JOptionPane.showMessageDialog(this, "客户端IP格式有误", "客户端IP错误", JOptionPane.ERROR_MESSAGE);
                return;
            }

            String port = portText.getText();
            if (StringUtils.isEmpty(port)) {
                JOptionPane.showMessageDialog(this, "请输入客户端端口", "客户端端口错误", JOptionPane.ERROR_MESSAGE);
                return;
            }

            if (!StringUtils.isNumeric(port) || Integer.parseInt(port) < 1 || Integer.parseInt(port) > 65535) {
                JOptionPane.showMessageDialog(this, "客户端端口格式不正确", "客户端端口错误", JOptionPane.ERROR_MESSAGE);
                return;
            }
            clientConfig.setHostName(hostName + (StringUtils.isNotEmpty(clientConfig.getDefaultHost()) ? clientConfig.getDefaultHost() : ""));
            clientConfig.setLocalPort(Integer.parseInt(port));
            clientConfig.setLocalHost(ip);
            setVisible(false);
        });
    }


    public ConfigWin(JFrame jFrame, ClientConfig clientConfig) {
        super(jFrame, "配置", false);
        this.clientConfig = clientConfig;
        init();
        setContentPane(contentPane);
        pack();
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        setVisible(true);
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        contentPane = new JPanel();
        contentPane.setLayout(new GridLayoutManager(3, 1, new Insets(10, 10, 10, 10), -1, -1));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
        contentPane.add(panel1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        final JLabel label1 = new JLabel();
        label1.setText("穿透域名");
        panel1.add(label1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 1, false));
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
        panel1.add(panel2, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        defaultHostLab = new JLabel();
        defaultHostLab.setText("bigant.club");
        panel2.add(defaultHostLab, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        hostNameField = new JTextField();
        panel2.add(hostNameField, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        final JPanel panel3 = new JPanel();
        panel3.setLayout(new GridLayoutManager(1, 4, new Insets(0, 0, 0, 0), -1, -1));
        contentPane.add(panel3, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        ipTextField = new JTextField();
        ipTextField.setText("192.168.201.90");
        panel3.add(ipTextField, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        final JLabel label2 = new JLabel();
        label2.setText("  ：");
        panel3.add(label2, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        portText = new JTextField();
        portText.setText("80");
        panel3.add(portText, new GridConstraints(0, 3, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        final JLabel label3 = new JLabel();
        label3.setText("   本地地址");
        panel3.add(label3, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JPanel panel4 = new JPanel();
        panel4.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        contentPane.add(panel4, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        提交Button = new JButton();
        提交Button.setText("提交");
        panel4.add(提交Button, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return contentPane;
    }

}

